/* Copyright (c) Soon.Liao 2024. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Create Date : 2024
 * @Date: 2024-10-14 07:57:06
 * @LastEditors: soon.liao
 * @LastEditTime: 2024-10-26 14:07:39
 * @FilePath: /src/application/ws63/rgb_master/keyValue/key_value.c
 * @Description: NA
 * NA
 */
#include "nv.h"
#include "key_value.h"

uint32_t key_value_write(uint16_t key, const uint8_t *kvalue, uint16_t kvalue_length)
{
    nv_key_attr_t attr = {0};
    uint16_t real_len = 0;
    uint32_t ret;

    ret = uapi_nv_get_key_attr(key, &real_len, &attr);
    if (ret != ERRCODE_SUCC) {
        return ret;
    }
    ret = uapi_nv_write_with_attr(key, kvalue, kvalue_length, &attr, NULL);
    if (ret != ERRCODE_SUCC) {
        return ret;
    }
    return ERRCODE_SUCC;
}

uint32_t key_value_read(uint16_t key, uint16_t kvalue_max_length, uint16_t *kvalue_length, uint8_t *kvalue)
{
    uint32_t ret;
    ret = uapi_nv_read(key, kvalue_max_length, kvalue_length, kvalue);
    return ret;
}

uint32_t key_value_delete(uint16_t key)
{
    uint32_t ret;
    ret = uapi_nv_delete_key(key);
    return ret;
}

uint32_t key_value_rgb_master_wifi_info_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_WIFI_INFO, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_wifi_info_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_WIFI_INFO, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_wifi_info_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_WIFI_INFO);
}

uint32_t key_value_rgb_master_on_off_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_ON_OFF, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_on_off_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_ON_OFF, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_on_onff_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_ON_OFF);
}

uint32_t key_value_rgb_master_led_number_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_LED_NUM, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_led_number_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_LED_NUM, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_led_number_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_LED_NUM);
}

uint32_t key_value_rgb_master_led_brightness_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_BRIGHTNESS, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_led_brightness_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_BRIGHTNESS, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_led_brightness_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_BRIGHTNESS);
}

uint32_t key_value_rgb_master_sence_id_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_SENCE_ID, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_sence_id_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_SENCE_ID, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_sence_id_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_SENCE_ID);
}

uint32_t key_value_rgb_master_mode_id_write(const uint8_t *kvalue, uint16_t kvalue_length)
{
    return key_value_write(NV_ID_RGB_MASTER_MODE_ID, kvalue, kvalue_length);
}

uint32_t key_value_rgb_master_mode_id_read(uint8_t *kvalue, uint16_t *kvalue_length)
{
    return key_value_read(NV_ID_RGB_MASTER_MODE_ID, NV_KEYVALUE_MAX_LENGTH, kvalue_length, kvalue);
}

uint32_t key_value_rgb_master_mode_id_delete(void)
{
    return key_value_delete(NV_ID_RGB_MASTER_MODE_ID);
}